/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ListaServicios.java
 *
 * Created on 24/11/2010, 01:57:32 AM
 */

package pe.edu.upla.lamerced.desk.view.maestro;

import java.util.List;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pe.edu.upla.lamerced.Inicial;
import pe.edu.upla.lamerced.bean.Servicios;
import pe.edu.upla.lamerced.service.ServiciosService;

/**
 *
 * @author elvis
 */
public class ListaServicios extends javax.swing.JInternalFrame {

   private JDesktopPane escritorio;
   private ServiciosService serviciosService;
   private List<Servicios> listaServicios;
    /** Creates new form ListaServicios */
    public ListaServicios() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
   // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {

      jLabel1 = new javax.swing.JLabel();
      jScrollPane1 = new javax.swing.JScrollPane();
      tblServicios = new javax.swing.JTable();
      btnEliminar = new javax.swing.JButton();
      btnModificar = new javax.swing.JButton();
      btnNuevo = new javax.swing.JButton();

      setClosable(true);
      setTitle("Lista Servicios");

      jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
      jLabel1.setText("Mantenimiento de Servicios");

      tblServicios.setModel(new javax.swing.table.DefaultTableModel(
         new Object [][] {
            {null, null, null, null, null}
         },
         new String [] {
            "Codigo", "Nombre", "Detalle", "Tarifa", "Tarificacion"
         }
      ));
      jScrollPane1.setViewportView(tblServicios);

      btnEliminar.setText("Eliminar");
      btnEliminar.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnEliminarActionPerformed(evt);
         }
      });

      btnModificar.setText("Modificar");
      btnModificar.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnModificarActionPerformed(evt);
         }
      });

      btnNuevo.setText("Nuevo");
      btnNuevo.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnNuevoActionPerformed(evt);
         }
      });

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addGap(97, 97, 97)
            .addComponent(jLabel1)
            .addContainerGap(109, Short.MAX_VALUE))
         .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE)
         .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(51, Short.MAX_VALUE)
            .addComponent(btnNuevo)
            .addGap(42, 42, 42)
            .addComponent(btnModificar)
            .addGap(54, 54, 54)
            .addComponent(btnEliminar)
            .addGap(40, 40, 40))
      );
      layout.setVerticalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(btnEliminar)
               .addComponent(btnModificar)
               .addComponent(btnNuevo))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE))
      );

      pack();
   }// </editor-fold>//GEN-END:initComponents

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        if (tblServicios.getSelectedRow()==-1){
         JOptionPane.showMessageDialog(this, "No ha seleccionado ningun servicio");
         return;
       }
       int res = JOptionPane.showConfirmDialog(this,"Seguro que desea eliminar el servicio?","Eliminar Servicio",JOptionPane.YES_NO_OPTION);
       if(res==0){
         Servicios dt = listaServicios.get(tblServicios.getSelectedRow());
         dt.setEstado(0);
         serviciosService.registrarServicio(dt);
         JOptionPane.showMessageDialog(this, "se ha eliminado el servicio seleccionado");
         listaServicios = serviciosService.listaServicios();
         pintagrilla();
       }
    }//GEN-LAST:event_btnEliminarActionPerformed

    private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed
        if (tblServicios.getSelectedRow()==-1){
          JOptionPane.showMessageDialog(this, "No ha seleccionado un Servicio");
         return;
       }
       FormServicios form = new FormServicios();
       form.setServicios(listaServicios.get(tblServicios.getSelectedRow()));
       form.setEscritorio(escritorio);
       escritorio.add(form);
       form.cargaInicial();
       form.setVisible(true);
       this.dispose();
    }//GEN-LAST:event_btnModificarActionPerformed

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
       // TODO add your handling code here:
       FormServicios form = new FormServicios();
       form.setServicios(new Servicios());
       form.setEscritorio(escritorio);
       escritorio.add(form);
       form.cargaInicial();
       form.setVisible(true);
       this.dispose();
    }//GEN-LAST:event_btnNuevoActionPerformed


   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JButton btnEliminar;
   private javax.swing.JButton btnModificar;
   private javax.swing.JButton btnNuevo;
   private javax.swing.JLabel jLabel1;
   private javax.swing.JScrollPane jScrollPane1;
   private javax.swing.JTable tblServicios;
   // End of variables declaration//GEN-END:variables
 public JDesktopPane getEscritorio() {
      return escritorio;
   }
   public void setEscritorio(JDesktopPane escritorio) {
      this.escritorio = escritorio;
   }

   public void cargainicial(){
      if(serviciosService == null){
         serviciosService = (ServiciosService)Inicial.getFactory().getBean("serviciosService");
      }
      if(listaServicios==null){
           listaServicios = serviciosService.listaServicios();
      }
      pintagrilla();
   }

   public void pintagrilla(){
        DefaultTableModel tabla = (DefaultTableModel) tblServicios.getModel();
        tabla.setRowCount(0);
        for (Servicios servicio : listaServicios) {
            Object[] fila = new Object[5];
            fila[0] = servicio.getIdservicios();
            fila[1] = servicio.getNombre();
            fila[2] = servicio.getDetalle();
            fila[3] = servicio.getTarifa();
            fila[4] = servicio.getTipotarificacion() == null ? "" :  servicio.getTipotarificacion().getNombre();
            tabla.addRow(fila);
        }

      }

}
